package com.sky.controller.admin;

import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController("adminDishController")
@Slf4j
@RequestMapping("/admin/dish")
@Api("菜品相关接口")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation( "新增菜品" )
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info( "新增菜品：{}",dishDTO );
        String key = "dish_"+dishDTO.getCategoryId();
        cleanCache( key );
        dishService.saveWithFlavor(dishDTO);
        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param dto
     * @return
     */
    @ApiOperation( "菜品分页查询" )
    @GetMapping("/page")
    //通过地址传参，不是JSON而是Query，所以不需要RequestBody
    public Result<PageResult> pageQuery( DishPageQueryDTO dto) {
        log.info( "菜品分页查询：{}",dto );
        //结果返回PageResult,总数和数据列表
        PageResult page =  dishService.pageQuery(dto);
        return  Result.success(page);
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @ApiOperation( "删除菜品" )
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {
        log.info( "删除菜品：{}",ids );
        cleanCache( "*dish_*" );
        dishService.delete(ids);
        return Result.success();
    }

    /**
     * 根据ID查询菜品
     * @param id
     * @return
     */
    @ApiOperation( "根据ID查询菜品" )
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info( "根据ID查询菜品：{}",id );
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation( "修改菜品" )
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info( "修改菜品：{}",dishDTO );
        cleanCache( "*dish_*" );
        dishService.update(dishDTO);
        return Result.success();
    }

    /**
     * 菜品起售停售
     * @param status
     * @return
     */
    @ApiOperation( "菜品起售停售" )
    @PostMapping("/status/{status}")
    public Result<String> updateDishStatus(@PathVariable Integer status,Long id) {
//        Long currentId = BaseContext.getCurrentId();
        log.info( "菜品起售停售：{}",status==1?"起售":"停售" );
        cleanCache( "*dish_*" );
        //根据当前id停售相应套餐，两者具有一致性，放入同一事务中
        dishService.updateDishStatue(status,id);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation( "根据分类id查询菜品" )
    public Result<List<Dish>> selectByCategoryId (Long categoryId){
        log.info( "根据分类id查询菜品：{}",categoryId );
        List<Dish> list = dishService.selectByCategoryId(categoryId);
        return Result.success(list);
    }
    private void cleanCache(String pattern) {
        Set keys = redisTemplate.keys( pattern );
        redisTemplate.delete( keys );
    }
}
